const http = require("http");
const fs = require("fs");

const server = http.createServer((req, res) => {
  req.setEncoding("binary"); // 设置请求体的编码格式

  const bounday = req.headers["content-type"]
    .split("; ")[1]
    .replace("boundary=", "");

  console.log(bounday);

  // 客户端传递的数据是表单数据(请求体)
  let formData = "";
  req.on("data", (data) => {
    formData += data;
  });

  req.on("end", () => {
    console.log(formData);
    // 1.截图从image/jpeg位置开始后面所有的数据
    const imgType = "image/jpeg";
    const imageTypePosition = formData.indexOf(imgType) + imgType.length;
    let imageData = formData.substring(imageTypePosition);

    // 2.imageData开始位置会有两个空格
    imageData = imageData.replace(/^\s\s*/, "");

    // 3.替换最后的boundary
    imageData = imageData.substring(0, imageData.indexOf(`--${bounday}--`));

    // 4.将imageData写入到文件中
    fs.writeFile("./baz.png", imageData, "binary", () => {
      console.log("imageData写入成功~");
      res.end("上传成功~");
    });
  });
});

server.listen(8000, () => {
  console.log("服务器启动成功~");
});
